// pages/cloudPatrol/detaily/AntiFraud/index.js
import {request,Modal} from "../../../../utils/util"
import {IMG_PATH,apiList,ApiRootUrl} from "../../../../config/index"
import Toast from '../../../../@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgCode:'',
    processCode:'',
    feedback:"",
    active:'',
    dataInfo:{},
    imgList:[],
    videoList:[],
    video_lA:'',
    img_lA:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this
    let userInfo=wx.getStorageSync('userinfo')
    console.log(userInfo);
    console.log(options);
    self.setData({
      orgCode:options.orgCode,
      processCode:options.processCode,
      active:options.active,
      userInfo:userInfo
    })
    self.getDetail()
  },
  addressblur(e){
    console.log(e);
    this.setData({
      address:e.detail.value
    })
  },
  //删除图片附件
  deleteAttachmentimg(e){

console.log(e);
let self = this;
wx.showModal({
  content: '是否删除该图片',
  confirmText:'确定',
  success (res) {
    if (res.confirm) {
      console.log('用户点击确定')
      let params = {
        filePath: e.currentTarget.dataset.filepath
      }
      Toast.loading({
        message: '加载中...',
        mask: true,
        duration: 0,
        forbidClick: true,
      });
      wx.request({
        url: `${ApiRootUrl}/cloudCheckTask/deleteAttachment`,
        method: 'GET',
        header: {
          'content-type': 'application/json; charset=utf-8',
          'cookie': wx.getStorageSync("sessionid")
        },
        params: params,
        data: params,
        success(res) {
          Toast.clear();
          if (res.data.code == 20000) {
            Toast({
              type: 'success',
              duration: 3000,
              message: '删除成功',
            });
            let newArr=[]
            self.data.imgList.forEach(item=>{
              console.log(item.attachmentAddr);
              console.log(e.currentTarget.dataset.filepath);
              if(item.attachmentAddr!=e.currentTarget.dataset.filepath){
                
                newArr.push(item)
              }
            })
            self.setData({
              imgList:newArr
            })
            console.log(newArr);
          }else {
            Toast({
              duration: 3000,
              forbidClick: true,
              type: 'fail',
              message: res.data.message,
              mask:true
            });
          }
        },
        fail(res) {
          Toast.clear();
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: '网络已断开',
          });
        }
      })
    }
  }
})

  },
  //删除视频附件
  deleteAttachmentvideo(e){

console.log(e);
let self = this;
wx.showModal({
  content: '是否删除该视频',
  confirmText:'确定',
  success (res) {
    if (res.confirm) {
      console.log('用户点击确定')
      let params = {
        filePath: e.currentTarget.dataset.filepath
      }
      Toast.loading({
        message: '加载中...',
        mask: true,
        duration: 0,
        forbidClick: true,
      });
      wx.request({
        url: `${ApiRootUrl}/cloudCheckTask/deleteAttachment`,
        method: 'GET',
        header: {
          'content-type': 'application/json; charset=utf-8',
          'cookie': wx.getStorageSync("sessionid")
        },
        params: params,
        data: params,
        success(res) {
          Toast.clear();
          if (res.data.code == 20000) {
            Toast({
              type: 'success',
              duration: 3000,
              message: '删除成功',
            });
            let newArr=[]
            self.data.videoList.forEach(item=>{
              console.log(item.attachmentAddr);
              console.log(e.currentTarget.dataset.filepath);
              if(item.attachmentAddr!=e.currentTarget.dataset.filepath){
                
                newArr.push(item)
              }
            })
            self.setData({
              videoList:newArr
            })
            console.log(newArr);
          }else {
            Toast({
              duration: 3000,
              forbidClick: true,
              type: 'fail',
              message: res.data.message,
              mask:true
            });
          }
        },
        fail(res) {
          Toast.clear();
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: '网络已断开',
          });
        }
      })
    }
  }
})

  },
  loadimgA: function () {
    Toast.loading({
      duration: 0,
      message: '上传中...',
      forbidClick: true,
    });
    var _this = this;
    wx.uploadFile({
      url: `${ApiRootUrl}/cloudCheckTask/uploadAttachment`, //接口
      filePath: _this.data.img_lA[0],
      name: 'file',
      formData: {
        'processCode': _this.data.processCode,
        'attachmentType':6,
        'file':_this.data.img_lA[0],
        'userId':_this.data.userInfo.userId
      },
      success: function (res) {
        Toast.clear();
          Toast({
            type: 'success',
            duration: 3000,
            message: '上传成功',
          });
        // var data = (JSON.stringify(res.data));
        var data=JSON.parse(res.data).data
        console.log(data);
        // return
        let imgList=_this.data.imgList
        let len=_this.data.imgList.length
        // let path=_this.data.imgList[len-1].imgpath
        // data.imgpath=path
        imgList[len-1]=data
        _this.setData({
          imgList:imgList
        })
      },
      fail: function (error) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: res.data.message,
        });
        console.log(error);
      }
    })
  },
  loadVideo: function () {
    Toast.loading({
      duration: 0,
      message: '上传中...',
      forbidClick: true,
    });
    var _this = this;
    wx.uploadFile({
      url: `${ApiRootUrl}/cloudCheckTask/uploadAttachment`, //接口
      filePath: _this.data.video_lA,
      name: 'file',
      formData: {
        'processCode': _this.data.processCode,
        'attachmentType':7,
        'file':_this.data.video_lA,
        'userId':_this.data.userInfo.userId
      },
      success: function (res) {
        Toast.clear();
          Toast({
            type: 'success',
            duration: 3000,
            message: '上传成功',
          });
        // var data = (JSON.stringify(res.data));
        var data=JSON.parse(res.data).data
        console.log(data);
        // return
        let videoList=_this.data.videoList
        let len=_this.data.videoList.length
        // let path=_this.data.imgList[len-1].imgpath
        // data.imgpath=path
        videoList[len-1]=data
        _this.setData({
          videoList:videoList
        })
      },
      fail: function (error) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: res.data.message,
        });
        console.log(error);
      }
    })
  },
  uploadPicture: function () {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(res)
        // return
        let arr=_this.data.imgList
        let obj={imgpath:res.tempFilePaths[0]}
        arr.push(obj)
        _this.setData({
          img_lA: res.tempFilePaths,
          imgList:arr
        },()=>{
          _this.loadimgA()
        })
        console.info(res.tempFilePaths.length);
      }
    })
  },
  uploadVideo: function () {
    var _this = this;
    wx.chooseVideo({
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(res)
        // return
        let arr=_this.data.videoList
        let obj={}
        arr.push(obj)
        _this.setData({
          video_lA: res.tempFilePath,
          videoList:arr
        },()=>{
          _this.loadVideo()
        })
      }
    })
  },
  getDetail(e) {
    let self = this;

      let params = {
        processCode: self.data.processCode,
        orgCode: self.data.orgCode
      }
      Toast.loading({
        message: '加载中...',
        mask: true,
        duration: 0,
        forbidClick: true,
      });
      wx.request({
        url: `${ApiRootUrl}/cloudCheckTask/queryCloudTaskDetail`,
        method: 'GET',
        header: {
          'content-type': 'application/json; charset=utf-8',
          'cookie': wx.getStorageSync("sessionid")
        },
        params: params,
        data: params,
        success(res) {
          Toast.clear();
          if (res.data.code == 20000) {
            console.log(res);
            let time=res.data.data.createTime
            let y=time.substring(0,4)
            let m=time.substring(4,6)
            let d=time.substring(6,8)
            let newTime=y+'-'+m+'-'+d
            res.data.data.createTime=newTime
            let imgList=[]
            let videoList=[]
            if(res.data.data.attachmentList&&res.data.data.attachmentList.length){
              res.data.data.attachmentList.forEach(item=>{
                if(item.attachmentType.toLowerCase().indexOf('video')!=-1){
                  videoList.push(item)
                }
                if(item.attachmentType.toLowerCase().indexOf('pic')!=-1){
                  imgList.push(item)
                }
              })
            }
            
            self.setData({
              dataInfo:res.data.data,
              imgList:imgList,
              videoList:videoList
            })
          }else {
            Toast({
              duration: 3000,
              forbidClick: true,
              type: 'fail',
              message: res.data.message,
              mask:true
            });
          }
        },
        fail(res) {
          Toast.clear();
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: '网络已断开',
          });
        }
      })
 
  },
  bindTextArea(e){
    console.log(e);
    this.setData({
      feedback:e.detail.value
    })
  },
  blurTextArea(e){
    console.log(e);
    this.setData({
      feedback:e.detail.value
    })
  },
  /**
   *  保存信息
   *   
   */
  update(){
    let self=this
    Toast.loading({
      duration: 0,
      message: '提交中...',
      forbidClick: true,
    });
    // if(!this.data.imgList||!this.data.imgList.length){
    //   Toast.clear();
    //       Toast({
    //         duration: 3000,
    //         type: 'fail',
    //         message:'请上传图片',
    //       });
    //       return
    // }
    if(!this.data.address){
      Toast.clear();
          Toast({
            duration: 3000,
            type: 'fail',
            message:'请输入地址',
          });
          return
    }
    if((!this.data.videoList||!this.data.videoList.length)&&(!this.data.imgList||!this.data.imgList.length)){
      Toast.clear();
          Toast({
            duration: 3000,
            type: 'fail',
            message:'请上传附件',
          });
          return
    }
    if(!this.data.feedback){
      Toast.clear();
          Toast({
            duration: 3000,
            type: 'fail',
            message:'请输入反馈内容',
          });
          return
    }
    let info=self.data.userInfo
    // console.log(info);
    // return
    let params={
      orgCode:info.componentCode,
      processCode:this.data.dataInfo.processCode,
      abnormalAddress:this.data.address,
      evaluateInfo:this.data.feedback,
    }
    wx.request({
      url: `${ApiRootUrl}/cloudCheckTask/dealAbnormalBehavior`,
      method: 'POST',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      success(res) {
        if(res.data.code == 20000){
          Toast.clear();
          Toast({
            type: 'success',
            duration: 3000,
            message: '提交成功',
          });
          setTimeout(()=>{
            wx.navigateBack()
          },1000) 
        }else{
          Toast.clear();
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})